<?php
    $this->headTitle($this->enterprise->company_name_vn.': '.$this->translator('blog_add_page_title'));

    $this->headLink()
    ->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.all.css');
$this->headScript()
    ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.validate/jquery.validate.min.js');
?>
<script type="text/javascript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/tinymce/tiny_mce.js"></script>
<style>
    .b_breadcrumb{

    }
    .b_bottom{
        margin-bottom: 10px;
    }
</style>
<div class="grid_12">
    <div class="grid_8 alpha">
        <?php if(!$this->found):?>
        <?php echo $this->translator('not_found_database');?>
        <?php else:?>
        <!--Sub menu-->
        <?php echo $this->widget('bizprofile','menu',array()); ?>
        <!--End sub menu-->
        <div class="subcolumns pb10">
            <div class="b_breadcrumb maintitle b_bottom">
                <?php
                    /**
                    * Param for url;
                    **/
                    $arrRouteList = array(
                        'enterprise_id' => $this->enterprise->id,
                        'enterprise_name' => Tomato_Utility_String::removeSign($this->enterprise->company_name_en, '-', true)
                    );
                ?>
                <a href="<?php echo $this->url($arrRouteList, 'b2b_blog_list');?>"><?php echo $this->translator('blog_list_breadcrumb');?></a> »
                <?php echo $this->translator('blog_edit_breadcrumb');?>
            </div>
            <?php if(!$this->allowAction):?>
                <?php echo $this->translator('blog_edit_access_denied');?>
            <?php else:?>
            <form action="" method="POST" id="editBlogForm">
                <div class="t_bizprofile_register">
                    <div class="flash_message"><?php  echo $this->flashMessenger(); ?></div>
                    <div class="clearfix">
                        <label><?php echo $this->translator('blog_label_language'); ?>:</label>
                        <?php echo $this->languageSelector(array(
                                            'id'   => 'language',
                                            'name' => 'language',
                                            'selected' => $this->blog->language,
                                            'disabled' => $this->blog->language
                                        ), null);
                        ?>
                    </div>
                    <div class="clearfix">
                        <label><font style="color:red; padding-right:5px;">(*)</font><?php echo $this->translator('blog_label_title');?>:</label>
                        <input value="<?php echo $this->blog->title;?>" type="text" name="title" id="title" style="width:300px;" />
                        <span id="invalid-title"></span>
                    </div>
                    <div class="clearfix">
                        <label><?php echo $this->translator('blog_label_slug');?>:</label>
                        <input value="<?php echo $this->blog->slug;?>" type="text" name="slug" id="slug" style="width:300px;" />
                        <span id="invalid-slug"></span>
                    </div>
                    <div class="clearfix">
                        <label><?php echo $this->translator('blog_label_link');?>:</label>
                        <input value="<?php echo $this->blog->link;?>" type="text" name="link" id="link" style="width:300px;" />
                    </div>
                    <div class="clearfix">
                        <label><font style="color:red; padding-right:5px;">(*)</font><?php echo $this->translator('blog_label_content');?>:</label>
                        <textarea name="content" id="content" class="tinymce" style="width: 515px; height: 200px"><?php echo $this->blog->content;?></textarea>
                        <label>&nbsp;</label>
                        <span id="invalid-content"></span>
                    </div>
                    <div class="clearfix">
                        <label>&nbsp;</label>
                        <input type="submit" value="<?php echo $this->translator('blog_btn_save');?>" name="btn_save"/>
                    </div>
                </div>
            </form>
            <?php endif;?>
        </div>
        <?php endif;?>
    </div>
    <div class="grid_4 omega">
        <?php echo $this->widget('bizprofile','map',array()); ?>
        <?php echo $this->widget('bizprofile','help',array()); ?>
    </div>
</div>
<div class="grid_12">
    <?php echo $this->widget('bizprofile','contact',array()); ?>
</div>

<script type="text/javascript">
    /**
    * Init TinyMCE instances
    */
    tinyMCE.init({
        theme: 'advanced',
        theme_advanced_toolbar_location: 'top',
        theme_advanced_toolbar_align: 'left',
        width: '515',
        height: '225',
        mode: 'exact',
        elements: 'content',
        /*content_css: '<?php echo $this->APP_STATIC_SERVER."/skins/default/".$this->APP_SKIN."/default.css";?>',*/
        relative_urls: false,
        remove_script_host: false
    });

    $(document).ready(function() {
        /**
        * Automatically create slug once user enter the title
        */
        $('#title').bind('change', function() {
            $('#slug').val(normalizeSlug($(this).val()));
        });

        /**
        * Validator add method
        */
        $.validator.addMethod('slug', function(value) {
            value = value.replace(/^\s+|\s+$/g, '');
            var regex = new RegExp(/^[a-z0-9\-]+$/);
            if (value == '' || value.match(regex)) {
                return true;
            } else {
                return false;
            }
        });

        /**
        * Jquery custom error placement
        **/
        jQuery.validator.setDefaults({
            errorPlacement: function(error, element) {
                error.appendTo('#invalid-' + element.attr('id'));
            }
        });

        $('#editBlogForm').validate({
            rules: {
                title: {
                    required: true,
                    maxlength: 255
                },
                slug: {
                    slug: true,
                    maxlength: 255
                },
                content: {
                    required: true,
                    minlength: 5
                },
            },
            messages: {
                title: {
                    required: "<?php echo $this->translator('blog_title_required'); ?>",
                    maxlength: "<?php echo $this->translator('blog_title_maxlength'); ?>"
                },
                slug: {
                    slug: "<?php echo $this->translator('blog_slug_validate'); ?>",
                    maxlength: "<?php echo $this->translator('blog_slug_maxlength'); ?>"
                },
                content: {
                    required: "<?php echo $this->translator('blog_content_required'); ?>",
                    minlength: "<?php echo $this->translator('blog_content_minlength'); ?>"
                },
            }
        });
    });

    function normalizeSlug(str) {
        str = str.replace(/^\s+|\s+$/g, '');
        var from = "ÁÀẠẢÃĂẮẰẶẲẴÂẤẦẬẨẪáàạảãăắằặẳẵâấầậẩẫóòọỏõÓÒỌỎÕôốồộổỗÔỐỒỘỔỖơớờợởỡƠỚỜỢỞỠéèẹẻẽÉÈẸẺẼêếềệểễÊẾỀỆỂỄúùụủũÚÙỤỦŨưứừựửữƯỨỪỰỬỮíìịỉĩÍÌỊỈĨýỳỵỷỹÝỲỴỶỸĐđÑñÇç·/_,:;";
        var to   = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaooooooooooooooooooooooooooooooooooeeeeeeeeeeeeeeeeeeeeeeuuuuuuuuuuuuuuuuuuuuuuiiiiiiiiiiyyyyyyyyyyddnncc------";

        for (var i = 0, l = from.length ; i < l; i++) {
            str = str.replace(new RegExp(from[i], "g"), to[i]);
        }
        str = str.replace(/[^a-zA-Z0-9 -]/g, '').replace(/\s+/g, '-').toLowerCase();
        return str;
    };
</script>
